package com.dzf.controller;

import com.dzf.entity.Blog;
import com.dzf.entity.Discuss;
import com.dzf.entity.RespJson;
import com.dzf.entity.Type;
import com.dzf.service.BlogService;
import com.dzf.service.DiscussService;
import com.dzf.service.TypeService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wcrc
 */
@Controller
@RequestMapping("/public")
public class PublicController {
    @Autowired
    BlogService blogService;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    DiscussService discussService;
    @Autowired
    TypeService typeService;

    Map map=new HashMap<>();


    /**
     * 获取博客列表
     * @param pageNum 页码
     * @param num 每页行数
     * @param likes   {msg：模糊查询关键字 search:决定是否进行模糊查询}
     * @return {total：总行数,list:博客列表}
     */
    @PostMapping("/findByPage/{pageNum}/{num}")
    @ResponseBody
    public RespJson findByPage(
            @PathVariable("pageNum") int pageNum,
            @PathVariable("num")int num,
            @RequestBody Map<String,Object> likes){
        System.out.println(pageNum);
        System.out.println(num);
        System.out.println("likes:"+likes);
        Map<String, Object> map = blogService.findFuzzy(pageNum, num, likes);
        return RespJson.success(200,"ok",map);
    }


    /**
     * 查看博客，
     * 把博客的id放在了成员变量map上，在进行跳转页面，
     * @param blogid 博客的ID
     * @return
     * @throws IOException
     */
    @RequestMapping("/findById/{blogid}")
    public String findById(@PathVariable("blogid") String blogid) throws IOException {
        map.put("id",blogid);
        return "/blog/findBlog";
    }

    /**
     * 通过成员变量中保存的博客id，得到博客的内容
     * @return blogCont 组合类对象
     * @throws IOException
     */
    @RequestMapping("/getBlog")
    @ResponseBody
    public RespJson getBlog() throws IOException {
        return RespJson.success(200,"OK",blogService.findById((String) map.get("id")));
    }

    /**
     * 博客排行榜
     * @param msg 进行排名的字段
     * @return 排序后的博客列表
     */
    @RequestMapping("/findByCTTop/{msg}")
    @ResponseBody
    public RespJson findByCreateTimeTop(@PathVariable("msg") String msg)
    {
        List<Blog> list = blogService.findByTops(msg);
        return RespJson.success(200,"ok",list);
    }

    /**
     * 获取某种类型的博客
     * @param typeid 博客类型iD
     * @param pagenum 页码
     * @return 返回blog列表
     */
    @RequestMapping("/blog/findByBlogType/{typeid}/{pagenum}")
    @ResponseBody
    public RespJson findByBlogType(@PathVariable("typeid") String typeid,@PathVariable("pagenum")int pagenum){
        System.out.println(typeid);
        System.out.println(pagenum);
        return  RespJson.success(200,"OK",blogService.findByBlogType(typeid,pagenum));
    }

    /**
     * 查询评论列表
     * @param blogid 博客的ID
     * @param pagenum 页码
     * num 定为六行
     * @return {total:总行数，list：查询的评论列表}
     */
    @RequestMapping("/dis/findByDis/{blogid}/{pagenum}")
    @ResponseBody
    public RespJson findByDis(@PathVariable("blogid")String blogid,@PathVariable("pagenum") int pagenum)
    {
        Map<String, Object> map = discussService.findByDis(blogid, pagenum);
        return RespJson.success(200,"OK",map);
    }

    /**
     * 获取一个父评论下的子评论
     * @param disid 评论对象的id
     * @param level 评论层级
     * @return discuss对象数组
     */
    @RequestMapping("/dis/findByDisLevel/{disid}/{level}")
    @ResponseBody
    public RespJson findByDisLevel(@PathVariable("disid")String disid,@PathVariable("level") int level)
    {
        List<Discuss> list = discussService.findByDislevel(disid, level);
        return RespJson.success(200,"OK",list);
    }

    /**
     * 类型全查询
     * @return 所有博客类型
     */
    @RequestMapping("/type/findAll")
    @ResponseBody
    public RespJson findAll(){
        List<Type> types = typeService.findAll();
        return RespJson.success(200,"类型获取成功",types);
    }
}
